{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "square-hydrogen",
   "metadata": {},
   "source": [
    "# Danish Citizen Numbers"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "technical-injection",
   "metadata": {},
   "source": [
    "## Introduction"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "indie-tackle",
   "metadata": {},
   "source": [
    "The function `clean_dk_cpr()` cleans a column containing Danish citizen number (CPR) strings, and standardizes them in a given format. The function `validate_dk_cpr()` validates either a single CPR strings, a column of CPR strings or a DataFrame of CPR strings, returning `True` if the value is valid, and `False` otherwise."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "isolated-bunch",
   "metadata": {},
   "source": [
    "CPR strings can be converted to the following formats via the `output_format` parameter:\n",
    "\n",
    "* `compact`: only number strings without any seperators or whitespace, like \"211062-5629\"\n",
    "* `standard`: CPR strings with proper whitespace in the proper places, like \"2110625629\"\n",
    "* `birthdate`: split the number and return the birth date, like \"1862-10-21\".\n",
    "\n",
    "Invalid parsing is handled with the `errors` parameter:\n",
    "\n",
    "* `coerce` (default): invalid parsing will be set to NaN\n",
    "* `ignore`: invalid parsing will return the input\n",
    "* `raise`: invalid parsing will raise an exception\n",
    "\n",
    "The following sections demonstrate the functionality of `clean_dk_cpr()` and `validate_dk_cpr()`. "
   ]
  },
  {
   "cell_type": "markdown",
   "id": "commercial-contact",
   "metadata": {},
   "source": [
    "### An example dataset containing CPR strings"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "proof-football",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "df = pd.DataFrame(\n",
    "    {\n",
    "        \"cpr\": [\n",
    "            \"2110625629\",\n",
    "            \"511062-5629\",\n",
    "            \"999 999 999\",\n",
    "            \"004085616\",\n",
    "            \"002 724 334\",\n",
    "            \"hello\",\n",
    "            np.nan,\n",
    "            \"NULL\",\n",
    "        ], \n",
    "        \"address\": [\n",
    "            \"123 Pine Ave.\",\n",
    "            \"main st\",\n",
    "            \"1234 west main heights 57033\",\n",
    "            \"apt 1 789 s maple rd manhattan\",\n",
    "            \"robie house, 789 north main street\",\n",
    "            \"1111 S Figueroa St, Los Angeles, CA 90015\",\n",
    "            \"(staples center) 1111 S Figueroa St, Los Angeles\",\n",
    "            \"hello\",\n",
    "        ]\n",
    "    }\n",
    ")\n",
    "df"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "square-consumer",
   "metadata": {},
   "source": [
    "## 1. Default `clean_dk_cpr`\n",
    "\n",
    "By default, `clean_dk_cpr` will clean cpr strings and output them in the standard format with proper separators."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "reliable-arnold",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import clean_dk_cpr\n",
    "clean_dk_cpr(df, column = \"cpr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "conservative-anchor",
   "metadata": {},
   "source": [
    "## 2. Output formats"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bound-organization",
   "metadata": {},
   "source": [
    "This section demonstrates the output parameter."
   ]
  },
  {
   "cell_type": "markdown",
   "id": "growing-technique",
   "metadata": {},
   "source": [
    "### `standard` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "civilian-likelihood",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_dk_cpr(df, column = \"cpr\", output_format=\"standard\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "starting-indicator",
   "metadata": {},
   "source": [
    "### `compact`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "israeli-tattoo",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_dk_cpr(df, column = \"cpr\", output_format=\"compact\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cellular-institution",
   "metadata": {},
   "source": [
    "### `birthdate`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "textile-barcelona",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_dk_cpr(df, column = \"cpr\", output_format=\"birthdate\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dressed-memory",
   "metadata": {},
   "source": [
    "## 3. `inplace` parameter\n",
    "\n",
    "This deletes the given column from the returned DataFrame. \n",
    "A new column containing cleaned CPR strings is added with a title in the format `\"{original title}_clean\"`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "beautiful-leone",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_dk_cpr(df, column=\"cpr\", inplace=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "narrow-portable",
   "metadata": {},
   "source": [
    "## 4. `errors` parameter"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "numeric-trainer",
   "metadata": {},
   "source": [
    "### `coerce` (default)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "expensive-zoning",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_dk_cpr(df, \"cpr\", errors=\"coerce\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fleet-cornwall",
   "metadata": {},
   "source": [
    "### `ignore`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "vocational-waters",
   "metadata": {},
   "outputs": [],
   "source": [
    "clean_dk_cpr(df, \"cpr\", errors=\"ignore\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "through-establishment",
   "metadata": {},
   "source": [
    "## 4. `validate_dk_cpr()`"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "shared-slovak",
   "metadata": {},
   "source": [
    "`validate_dk_cpr()` returns `True` when the input is a valid CPR. Otherwise it returns `False`.\n",
    "\n",
    "The input of `validate_dk_cpr()` can be a string, a Pandas DataSeries, a Dask DataSeries, a Pandas DataFrame and a dask DataFrame.\n",
    "\n",
    "When the input is a string, a Pandas DataSeries or a Dask DataSeries, user doesn't need to specify a column name to be validated. \n",
    "\n",
    "When the input is a Pandas DataFrame or a dask DataFrame, user can both specify or not specify a column name to be validated. If user specify the column name, `validate_dk_cpr()` only returns the validation result for the specified column. If user doesn't specify the column name, `validate_dk_cpr()` returns the validation result for the whole DataFrame."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "offshore-native",
   "metadata": {},
   "outputs": [],
   "source": [
    "from dataprep.clean import validate_dk_cpr\n",
    "print(validate_dk_cpr(\"2110625629\"))\n",
    "print(validate_dk_cpr(\"511062-5629\"))\n",
    "print(validate_dk_cpr(\"999 999 999\"))\n",
    "print(validate_dk_cpr(\"51824753556\"))\n",
    "print(validate_dk_cpr(\"004085616\"))\n",
    "print(validate_dk_cpr(\"hello\"))\n",
    "print(validate_dk_cpr(np.nan))\n",
    "print(validate_dk_cpr(\"NULL\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ranging-sleep",
   "metadata": {},
   "source": [
    "### Series"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "overhead-concentration",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_dk_cpr(df[\"cpr\"])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dutch-maker",
   "metadata": {},
   "source": [
    "### DataFrame + Specify Column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "disciplinary-america",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_dk_cpr(df, column=\"cpr\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "close-sunrise",
   "metadata": {},
   "source": [
    "### Only DataFrame"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "classified-vision",
   "metadata": {},
   "outputs": [],
   "source": [
    "validate_dk_cpr(df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "induced-advisory",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
